package team.seekers.hula.system.resource.controller;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.exceptions.ClientException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import team.seekers.hula.common.annotation.ApiResponseBody;
import team.seekers.hula.system.resource.config.FileSystemProperties;
import team.seekers.hula.system.resource.entity.dto.AliDemandDTO;
import team.seekers.hula.system.resource.service.DemandService;
import team.seekers.hula.system.resource.util.OssUtil;

import javax.annotation.Resource;
import java.util.Map;

/**
 * date: 2022/4/25 21:33
 *
 * @author GH
 * QQ 1124813753
 */
@Slf4j
@Controller
@ApiResponseBody
@RequestMapping("/demand")
public class DemandController {

    @Autowired
    private DemandService demandService;

    @Resource
    private FileSystemProperties fileSystemProperties;

    /**
     * 获取oss临时授权
     * @param type 授权类型，图片类型或普通文件类型
     */
    @GetMapping("/uploadFile")
    public JSONObject getCredit(@RequestParam String type) {
        JSONObject jsonObject = new JSONObject();
        JSONObject creditInfo;
        String basePath;
        switch (type) {
            case "image":
                basePath = "image";
                break;
            default:
                basePath = "file";
        }
        try {
            creditInfo = OssUtil.getCredit(
                    "hula-hbc",
                    fileSystemProperties.getOssRoleArn(),
                    fileSystemProperties.getOssAccessKeyId(),
                    fileSystemProperties.getOssAccessKeySecret(),
                    fileSystemProperties.getOssBucket());
            //文件存放地域
            jsonObject.put("region", fileSystemProperties.getOssRegion());
            //临时访问accessKey
            jsonObject.put("accessKeyId", creditInfo.getString(OssUtil.ACCESS_KEY_ID));
            //临时访问accessKeySecret
            jsonObject.put("accessKeySecret", creditInfo.getString(OssUtil.ACCESS_KEY_SECRET));
            //临时访问
            jsonObject.put("securityToken", creditInfo.getString(OssUtil.SECURITY_TOKEN));
            //临时访问过期时间
            jsonObject.put("expiration", creditInfo.getString(OssUtil.EXPIRATION));
            //bucket名称
            jsonObject.put("bucket", fileSystemProperties.getOssBucket());
            //文件的存放基目录
            jsonObject.put("basePath", "/");
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return jsonObject;
    }






    @PostMapping("/uploadVideo")
    public AliDemandDTO uploadVideo(@RequestBody Map<String, String> json) {
        return demandService.uploadVideo(json.get("title"), json.get("filename"));
    }

    @PostMapping("/refreshVideoAuth")
    public AliDemandDTO refreshUploadVideo(@RequestParam String videoId) {
        return demandService.refreshUploadVideo(videoId);
    }

    @PostMapping("/uploadImage")
    public AliDemandDTO uploadImage(@RequestParam String imgName,
                         @RequestParam String imgExt) {
        return demandService.uploadImage(imgName, imgExt);
    }

    @GetMapping("/getVideoAuth")
    public AliDemandDTO getVideoPlayAuth(@RequestParam String videoId) {
        return demandService.getVideoPlayAuth(videoId);
    }

    @PostMapping("/uploadMedia")
    public AliDemandDTO uploadAttachedMedia(@RequestParam String bType,
                                 @RequestParam String mediaName,
                                 @RequestParam String mediaExt) {
        return demandService.uploadAttachedMedia(bType, mediaName, mediaExt);
    }
}
